package view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.Author;
import model.Conference;

import controller.DataBaseInteactor;

/**
 * Make a frame to view the manuscript.
 * 
 * @author Richard Lee
 * @version 61213
 */
public class ViewView extends JFrame {
	
	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = 7635790085806708171L;
	
	/**
	 * GUI component.
	 */
	private final JPanel bottomPanel = new JPanel(new FlowLayout());
	private final JPanel titlePanel = new JPanel(new FlowLayout());
	private final JPanel authorPanel = new JPanel(new FlowLayout());
	private final JPanel bodyPanel = new JPanel(new FlowLayout());
	private final JLabel titleLabel = new JLabel("Title");
	private final JLabel authorLabel = new JLabel("Author");
	private final JLabel bodyLabel = new JLabel("Body");
	private final JTextField titleField = new JTextField(30);
	private final JTextField authorField = new JTextField(30);
	private final JTextArea bodyField = new JTextArea(10, 30);
	private final JButton cancelButton = new JButton("Cancel");
	
	/**
	 * Database interactor
	 */
	private DataBaseInteactor interactor;
	
	/**
	 * Author user id.
	 */
	private final String authorUN;
	
	/**
	 * Title of the manuscript.
	 */
	private final String title;
	
	/**
	 * Conference user belongs to.
	 */
	private final Conference conf;
	
	/**
	 * The Author.
	 */
	private final Author author;
	
	/**
	 * Constructor.
	 * 
	 * @param theAuthor the author of the manuscript.
	 * @param theTitle the title of the manuscript.
	 * @param theConf conference where author belongs to.
	 * @param theInteactor database interactor.
	 */
	public ViewView(String theAuthor, String theTitle, Conference theConf, DataBaseInteactor theInteactor) {
		super("View Manscript");
		interactor = theInteactor;
		authorUN = theAuthor;
		title = theTitle;
		conf = theConf;
		author = new Author(authorUN, interactor);
	}
	
	/**
	 * Combine and add all the GUI components.
	 */
	public void createViewView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		titlePanel.add(titleLabel);
		titlePanel.add(titleField);
		titleField.setText(title);
		authorPanel.add(authorLabel);
		authorPanel.add(authorField);
		authorField.setText(authorUN);
		bodyPanel.add(bodyLabel);
		bodyPanel.add(bodyField);
		bodyField.setText(interactor.getManuscriptContents(author, conf, title));
		bodyField.setBorder(BorderFactory.createLineBorder(Color.black));
		bottomPanel.add(cancelButton);
		p.add(titlePanel);
		p.add(authorPanel);
		p.add(bodyPanel);
		p.add(bottomPanel);
		add(p);
		setVisible(true);
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * add actions to buttons.
	 */
	private void actionButton() {
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}
}
